package windows.propertyWindow
{
import windows.*;
	import flash.display.Sprite;
	import com.bit101.components.RadioButton;
	import flash.events.Event;
	
	import com.bit101.components.Window;
	import com.bit101.components.Label;
	import com.bit101.components.InputText;
	import com.bit101.components.CheckBox;

	import baseitems.Base;

	import windows.AlignView;
	import windows.propertyWindow.PropertyLayerView;
	import flash.events.MouseEvent;
	import property.ItemType;
	import property.Align;
	/**
	 * общие пропертя - положение якорь ширинавысота%
	 * @author artem.kolesnikov 
	 */
	public class PropertyNodeView extends Sprite 
	{
		private var myLabel1:Label;
		private var myLabel2:Label;

		private var alignView1:AlignView;
		private var alignView2:AlignView;

		private var widthCheckBox:CheckBox;
		private var heightCheckBox:CheckBox;
		private var widthInput:InputText;
		private var heightInput:InputText;
		
		private var nameInput:InputText;
		
		public function PropertyNodeView() 
		{
			myLabel1 = new Label(this, 10, 5, "");

			alignView1 = new AlignView("align");
			alignView1.x = 10;
			alignView1.y = 25;
			alignView1.groupName = "position_align";
			alignView1.addEventListener(MouseEvent.CLICK, onAlignClick);
			addChild(alignView1);
			
			alignView2 = new AlignView("anchor");
			alignView2.x = alignView1.x + alignView1.width + 10;
			alignView2.y = alignView1.y;
			alignView2.groupName = "inner_align";
			alignView2.addEventListener(MouseEvent.CLICK, onAlignClick);
			addChild(alignView2);
			
			widthCheckBox = new CheckBox(this, 10, 100, "width%", onWidthHeightCheckBoxChange);
			widthInput = new InputText(this, 10, 110, "50", onWidthHeightTextChange);
			widthInput.width = 45;
			heightCheckBox = new CheckBox(this, 65, 100, "height%", onWidthHeightCheckBoxChange);
			heightInput = new InputText(this, 65, 110, "50", onWidthHeightTextChange);
			heightInput.width = 45;
			
			nameInput = new InputText(this, 10, 135, "", onWidthHeightTextChange);
		}
		
		private function onAlignClick(event:MouseEvent):void {
			if (Controller.instance.haveSelected) {
				Controller.instance.selectedObject.posAlign = alignView1.getAlign();
				Controller.instance.selectedObject.anchor = alignView2.getAlign();
			}
		}
		
		private function onWidthHeightCheckBoxChange(event:MouseEvent):void {
			widthInput.enabled = widthCheckBox.selected;
			heightInput.enabled = heightCheckBox.selected;
			
			onWidthHeightTextChange(event);
		}
		
		private function refresh():void {
			var obj:Base = Controller.instance.selectedObject;
			myLabel1.text = "pos: " + obj.x + "x" + obj.y + " [" + obj.width + "x" + obj.height + "]";
			
			alignView1.setAlign(obj.posAlign);
			alignView2.setAlign(obj.anchor);
			
			widthCheckBox.selected = obj.percentWidthEnabled;
			widthInput.enabled = obj.percentWidthEnabled;
			widthInput.text = obj.percentWidth.toString();
			heightCheckBox.selected = obj.percentHeightEnabled;
			heightInput.enabled = obj.percentHeightEnabled;
			heightInput.text = obj.percentHeight.toString();
			
			nameInput.text = obj.name;
		}
		
		private function onWidthHeightTextChange(event:Event):void {
			if (Controller.instance.haveSelected) {
				Controller.instance.selectedObject.percentWidthEnabled = widthCheckBox.selected;
				Controller.instance.selectedObject.percentWidth = int( widthInput.text);
				
				Controller.instance.selectedObject.percentHeightEnabled = heightCheckBox.selected;
				Controller.instance.selectedObject.percentHeight = int( heightInput.text);
				Controller.instance.selectedObject.name = nameInput.text;
			}
		}
		
		override public function set visible(value:Boolean) :void {
			super.visible = value;
			if (value) {
				refresh();
			}
		}
		
	}
}